package com.xb.test.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xb.test.entity.Data;
import com.xb.test.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@RestController
@RequestMapping("/log")
public class DataController {

    public static Logger logger = LoggerFactory.getLogger(DataController.class);


    @RequestMapping(value = {"/data"},method = RequestMethod.GET)
    public void data(HttpServletRequest req) throws Exception{

        String planid = req.getParameter("planid");
        String adsid = req.getParameter("adsid");
        String zoneid = req.getParameter("zoneid");
        String plantype = req.getParameter("plantype");
        String siteid = req.getParameter("siteid");
        String site_page = req.getParameter("site_page");
        String useragent = req.getHeader("User-Agent");
        String screen = req.getParameter("screen");
        String browser = BrowserUtils.getBrowser(useragent).brand;
        String ip = IPAddrUtils.getIpAddr(req);
        String country = "";
        String province = "";
        String city = "";
        String isp = "";
        String lon = "";
        String lat = "";
        if(ip.length()>0 && ip.length()<=15 && !ip.equals("unknown") && !ip.equals("undefined")) {
            JSONArray jsonIP = IPparseUtils.getArea(ip);
            if (jsonIP.size() > 1) {
                try {
                    country = jsonIP.get(1).toString();
                    province = jsonIP.get(2).toString();
                    city = jsonIP.get(3).toString();
                    isp = jsonIP.get(5).toString();
                    lon = jsonIP.get(9).toString();
                    lat = jsonIP.get(10).toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else{
            country = "";
            province = "";
            city = "";
            isp = "";
            lon = "";
            lat = "";
        }
        String day = DateUtils.getString2(Long.valueOf(System.currentTimeMillis()),"yyyy-MM-dd");
        String addtime = DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss", Long.valueOf(System.currentTimeMillis()));
        Date time = Calendar.getInstance().getTime();


        String uuid = req.getParameter("uuid");


        Data data = new Data();
        data.setPlanid(planid);
        data.setAdsid(adsid);
        data.setZoneid(zoneid);
        data.setPlantype(plantype);
        data.setSiteid(siteid);
        data.setSite_page(site_page);
        data.setUseragent(useragent);
        data.setScreen(screen);
        data.setBrowser(browser);
        data.setIp(ip);
        data.setCountry(country);
        data.setProvince(province);
        data.setCity(city);
        data.setIsp(isp);
        data.setLon(lon);
        data.setLat(lat);
        data.setDay(day);
        data.setAddtime(addtime);
        data.setUuid(uuid);
        data.setTime(time);

        String result = JSON.toJSONString(data);

        //Object A = JSONArray.toJSON(data);
        //String result = JSONArray.toJSONString(A);


        boolean code = KafkaUtils.sendMessage(result, "test_logs");
//         logger.error("计数器");

//        System.out.println(result);
//        return result;


    }


    /**
     * 点击上报数据接口
     */
    @RequestMapping(value = {"/checkData"},method = RequestMethod.GET)
    public void checkData(HttpServletRequest req) throws Exception{

        String sid = req.getParameter("sid") == null ? "" : req.getParameter("sid");
        String referrer = req.getParameter("referrer") == null ? "" : req.getParameter("referrer");
        String ua = req.getParameter("ua") == null ? "" : req.getParameter("ua");
        String isJSBridge = req.getParameter("isJSBridge");
        String batteryCharging = req.getParameter("batteryCharging");
        String batteryLevel = req.getParameter("batteryLevel");
        String touch = req.getParameter("touch");
        String platform = req.getParameter("platform");
        String vendor = req.getParameter("vendor");
        String renderer = req.getParameter("renderer");
        String finger = req.getParameter("finger");
        String type = req.getParameter("type");//2小说

        String ip = IPAddrUtils.getIpAddr(req);

        String day = DateUtils.getString2(Long.valueOf(System.currentTimeMillis()),"yyyy-MM-dd");
        String addtime = DateUtils.getSystemTime("yyyy-MM-dd HH:mm:ss", Long.valueOf(System.currentTimeMillis()));
        Date time = Calendar.getInstance().getTime();


        Data data = new Data();
        data.setSid(sid);
        data.setReferrer(referrer);
        data.setUseragent(ua);
        data.setIsJSBridge(isJSBridge);
        data.setBatteryCharging(batteryCharging);
        data.setBatteryLevel(batteryLevel);
        data.setTouch(touch);
        data.setPlatform(platform);
        data.setVendor(vendor);
        data.setRenderer(renderer);
        data.setFinger(finger);
        data.setDay(day);
        data.setAddtime(addtime);
        data.setTime(time);
        data.setIp(ip);
        data.setType(type);

        String result = JSON.toJSONString(data);
        logger.error(result);
        boolean code = KafkaUtils.sendMessage(result, "check_logs");


    }




}
